package com.mycompany.xxy_dome.util;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SettingsPage{

    private static JButton soundButton;
    private static JButton doNotDisturbButton;
    private static JButton energyButton;

    private static boolean isSoundOn = true;
    private static boolean isDoNotDisturbOn = true;
    private static int energyLevel = 3; // 3 levels: full, medium, low

    public static JPanel getJPanel() {

        // 创建主面板
        JPanel mainPanel = new JPanel(new GridBagLayout());
        mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); // 外部边距

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5); // 组件之间的间距

        // 创建音效按钮
        soundButton = createButtonRow(mainPanel, gbc, "Sound", "Toggle sound on/off", isSoundOn ? Color.GREEN : Color.RED, 0);
        soundButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                isSoundOn = !isSoundOn;
                updateButtonColor(soundButton, isSoundOn ? Color.GREEN : Color.RED);
                System.out.println("Sound button clicked: " + (isSoundOn ? "ON" : "OFF"));
            }
        });

        // 创建勿扰按钮
        doNotDisturbButton = createButtonRow(mainPanel, gbc, "Do Not Disturb", "Toggle do not disturb mode", isDoNotDisturbOn ? Color.GREEN : Color.RED, 1);
        doNotDisturbButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                isDoNotDisturbOn = !isDoNotDisturbOn;
                updateButtonColor(doNotDisturbButton, isDoNotDisturbOn ? Color.GREEN : Color.RED);
                System.out.println("Do Not Disturb button clicked: " + (isDoNotDisturbOn ? "ON" : "OFF"));
            }
        });

        // 创建能量按钮
        energyButton = createButtonRow(mainPanel, gbc, "Energy", "Adjust energy level", getEnergyColor(energyLevel), 2);
        energyButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                energyLevel = (energyLevel - 1 + 3) % 3; // 循环3个级别
                updateButtonColor(energyButton, getEnergyColor(energyLevel));
                System.out.println("Energy button clicked: Level " + energyLevel);
            }
        });

        return mainPanel;
    }

    private static JButton createButtonRow(JPanel mainPanel, GridBagConstraints gbc, String buttonText, String description, Color color, int row) {
        // 创建不可编辑的文本框
        JTextField nameField = new JTextField(buttonText);
        nameField.setEditable(false);
        nameField.setBorder(BorderFactory.createEmptyBorder());
        nameField.setBackground(mainPanel.getBackground());
        nameField.setForeground(Color.BLACK);
        nameField.setFont(new Font("Arial", Font.BOLD, 14));

        // 创建胶囊状按钮
        JButton button = createCapsuleButton(color);
        button.setFocusPainted(false);

        // 创建描述标签
        JLabel descriptionLabel = new JLabel(description);
        descriptionLabel.setFont(new Font("Arial", Font.PLAIN, 12));
        descriptionLabel.setForeground(Color.DARK_GRAY);

        // 设置布局约束
        gbc.gridy = row;
        gbc.gridx = 0;
        gbc.weightx = 0.1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        mainPanel.add(nameField, gbc);

        gbc.gridx = 1;
        gbc.weightx = 0.1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        mainPanel.add(button, gbc);

        gbc.gridx = 2;
        gbc.weightx = 0.8;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        mainPanel.add(descriptionLabel, gbc);

        return button; // 返回按钮以便后续操作
    }

    private static JButton createCapsuleButton(Color color) {
        JButton button = new JButton() {
            @Override
            protected void paintComponent(Graphics g) {
                g.setColor(getBackground());
                g.fillRoundRect(0, 0, getWidth(), getHeight(), 20, 20); // 胶囊状按钮
                super.paintComponent(g);
            }

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(80, 30); // 设置固定大小
            }
        };
        button.setBackground(color);
        button.setOpaque(true);
        button.setBorder(BorderFactory.createEmptyBorder(5, 20, 5, 20)); // 内部边距
        button.setContentAreaFilled(false);
        button.setBorderPainted(false);
        return button;
    }

    private static void updateButtonColor(JButton button, Color color) {
        button.setBackground(color);
    }

    private static Color getEnergyColor(int level) {
        switch (level) {
            case 0:
                return new Color(153, 255, 153); // 淡绿色
            case 1:
                return new Color(0, 204, 0); // 中等绿色
            case 2:
                return Color.GREEN; // 深绿色
            default:
                return Color.GREEN;
        }
    }
}